package cn.edu.zafu.bigdata.XuShuXin.Exp1210;

import java.sql.*;
import java.util.Scanner;

/**
 * @author: aluvfy
 * @description:  功能实现
 * @date: 2024/12/10 10:39
 */

public class StudentInfoManagementSystem_xsx {

    private static final String DB_URL = "jdbc:mysql://localhost:3306/jdbc";
    private static final String DB_USER = "root";
    private static final String DB_PASSWORD = "root";

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("======= 学生信息管理系统 =======");
            System.out.println("1. 添加学生信息");
            System.out.println("2. 检索学生信息");
            System.out.println("3. 更新学生信息");
            System.out.println("4. 删除学生信息");
            System.out.println("5. 退出程序");
            System.out.print("请输入操作编号 (1-5): ");

            int choice = scanner.nextInt();
            scanner.nextLine(); // consume newline

            switch (choice) {
                case 1:
                    addStudent(scanner);
                    break;
                case 2:
                    retrieveStudent(scanner);
                    break;
                case 3:
                    updateStudent(scanner);
                    break;
                case 4:
                    deleteStudent(scanner);
                    break;
                case 5:
                    System.out.println("退出程序");
                    scanner.close();
                    System.exit(0);
                default:
                    System.out.println("无效的操作编号，请重新输入");
            }
        }
    }

    private static Connection connect() throws SQLException, ClassNotFoundException {
        Class.forName("com.mysql.cj.jdbc.Driver");
        return DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
    }

    private static void addStudent(Scanner scanner) {
        System.out.print("请输入学号: ");
        String id = scanner.nextLine();
        System.out.print("请输入姓名: ");
        String name = scanner.nextLine();
        System.out.print("请输入电话: ");
        String phone = scanner.nextLine();

        String checkSql = "SELECT * FROM students WHERE id = ?";
        String insertSql = "INSERT INTO students (id, name, phone) VALUES (?, ?, ?)";

        try (Connection conn = connect();
             PreparedStatement checkStmt = conn.prepareStatement(checkSql);
             PreparedStatement insertStmt = conn.prepareStatement(insertSql)) {

            checkStmt.setString(1, id);
            ResultSet rs = checkStmt.executeQuery();
            if (rs.next()) {
                System.out.println("学号重复，请重新输入。");
                return;
            }

            insertStmt.setString(1, id);
            insertStmt.setString(2, name);
            insertStmt.setString(3, phone);
            insertStmt.executeUpdate();
            System.out.println("学生信息添加成功！");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void retrieveStudent(Scanner scanner) {
        while (true) {
            System.out.print("请输入要检索的学号 (输入 'QUIT' 退出): ");
            String id = scanner.nextLine();
            if (id.equalsIgnoreCase("QUIT")) {
                break;
            }

            String sql = "SELECT * FROM students WHERE id = ?";
            try (Connection conn = connect();
                 PreparedStatement stmt = conn.prepareStatement(sql)) {

                stmt.setString(1, id);
                ResultSet rs = stmt.executeQuery();
                if (rs.next()) {
                    System.out.println("学号: " + rs.getString("id"));
                    System.out.println("姓名: " + rs.getString("name"));
                    System.out.println("电话: " + rs.getString("phone"));
                } else {
                    System.out.println("未找到该学生");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private static void updateStudent(Scanner scanner) {
        while (true) {
            System.out.print("请输入要更新的学生学号 (输入 'QUIT' 退出): ");
            String id = scanner.nextLine();
            if (id.equalsIgnoreCase("QUIT")) {
                break;
            }

            System.out.print("请输入新姓名: ");
            String name = scanner.nextLine();
            System.out.print("请输入新电话: ");
            String phone = scanner.nextLine();

            String sql = "UPDATE students SET name = ?, phone = ? WHERE id = ?";
            try (Connection conn = connect();
                 PreparedStatement stmt = conn.prepareStatement(sql)) {

                stmt.setString(1, name);
                stmt.setString(2, phone);
                stmt.setString(3, id);
                int rowsUpdated = stmt.executeUpdate();
                if (rowsUpdated > 0) {
                    System.out.println("学生信息更新成功！");
                } else {
                    System.out.println("未找到该学生");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private static void deleteStudent(Scanner scanner) {
        while (true) {
            System.out.print("请输入要删除的学生学号 (输入 'QUIT' 退出): ");
            String id = scanner.nextLine();
            if (id.equalsIgnoreCase("QUIT")) {
                break;
            }

            String sql = "DELETE FROM students WHERE id = ?";
            try (Connection conn = connect();
                 PreparedStatement stmt = conn.prepareStatement(sql)) {

                stmt.setString(1, id);
                int rowsDeleted = stmt.executeUpdate();
                if (rowsDeleted > 0) {
                    System.out.println("学生信息删除成功！");
                } else {
                    System.out.println("未找到该学生");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
